package com.ctg.behavior.calc.impala.transfer;

import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.utils.CtCalcSQLUtils;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcCondition;
import com.ctg.behavior.common.calc.CtCalcFilter;
import com.ctg.behavior.common.consts.CtRelationConsts;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtFilterTransfer extends CtConditionTransfer {

	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {
		if (!(calcObject instanceof CtCalcFilter)) {
			log.warn("calcObject is not CtCalcFilter, class={}", calcObject.getClass());
			return "";
		}
		return this.doBizTransfer(calcObject, sqlParam);
	}

	private String doBizTransfer(CtCalc calcObject, CtSQLParam sqlParam) {

		CtCalcFilter filter = (CtCalcFilter) calcObject;
		log.info("CalcBody={}", JSON.toJSONString(filter));

		StringBuilder impalaSQL = new StringBuilder();
		impalaSQL.append(" ( ");

		// transfer for conditions
		List<CtCalcCondition> conditions = filter.getConditions();
		for (int index = 0; index < conditions.size(); index++) {
			CtCalcCondition oneCondition = conditions.get(index);
			// transfer one condition
			String tempSQL = super.doTransfer(oneCondition, sqlParam);
			if(CtCalcSQLUtils.isEmptyCondtions(tempSQL)) {
				continue;
			}
			impalaSQL.append(" ").append(tempSQL).append(" ");
			if (index == conditions.size() - 1) {
				continue;
			}
			if (CtRelationConsts.KEY_AND.equalsIgnoreCase(filter.getRelation())) {
				impalaSQL.append(CtRelationConsts.KEY_AND);
			} else if (CtRelationConsts.KEY_OR.equalsIgnoreCase(filter.getRelation())) {
				impalaSQL.append(CtRelationConsts.KEY_OR);
			} else {
				log.error("filter.getRelation()={}", filter.getRelation());
				log.info("CalcSQL={}", impalaSQL.toString());
				return null;
			}
		}

		// transfer for filters
		List<CtCalcFilter> filters = filter.getFilters();
		if (null == filters || filters.isEmpty()) {
			impalaSQL.append(" ) ");
			String retSQL = impalaSQL.toString();
			if (CtCalcSQLUtils.isEmptyCondtions(retSQL)) {
				retSQL = "";
			}
			log.info("CalcSQL={}", impalaSQL.toString());
			return retSQL;
		}
		// filters 不为空， 则执行行递归
		for (int index = 0; index < filters.size(); index++) {
			CtCalcFilter oneFilter = filters.get(index);
			String oneFilterSQL = this.doBizTransfer(oneFilter, sqlParam);
			if (null == oneFilterSQL) {
				// 解析存在问题，直接返回
				log.info("CalcSQL={}", impalaSQL.toString());
				return null;
			}
			if (index == conditions.size() - 1) {
				continue;
			}
			if(CtCalcSQLUtils.isEmptyCondtions(oneFilterSQL)) {
				continue;
			}
			if (CtRelationConsts.KEY_AND.equalsIgnoreCase(filter.getRelation())) {
				impalaSQL.append(CtRelationConsts.KEY_AND);
			} else if (CtRelationConsts.KEY_OR.equalsIgnoreCase(filter.getRelation())) {
				impalaSQL.append(CtRelationConsts.KEY_OR);
			} else {
				log.error("filter.getRelation()={}", filter.getRelation());
				log.info("CalcSQL={}", impalaSQL.toString());
				return null;
			}
			impalaSQL.append(" ").append(oneFilterSQL);
		}

		impalaSQL.append(" ) ");

		String retSQL = impalaSQL.toString();
		if (CtCalcSQLUtils.isEmptyCondtions(retSQL)) {
			retSQL = "";
		}
		log.info("CalcSQL={}", retSQL);
		return retSQL;

	}
}
